/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anhndt.audiobook.ui;

import com.anhndt.audiobook.business.Resource;
import com.anhndt.audiobook.business.Service;
import com.anhndt.audiobook.entity.ItemEntity;
import com.anhndt.audiobook.parser.WebServicesParser;
import com.anhndt.audiobook.utils.DBUtil;
import com.anhndt.audiobook.utils.DialogUtil;
import com.anhndt.audiobook.utils.StringUtil;
import com.anhndt.audiobook.utils.Util;
import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import org.ksoap2.serialization.SoapObject;

/**
 *
 * @author Loki
 */
public final class ForgotPasswordForm extends Form implements ActionListener {

    Button mButtonGetPassword = new Button(Resource.CMD_FORGOT_PASSWD);
    TextField mEmail = new TextField();
    Command mCommandBack = new Command(Resource.CMD_BACK);
    Command mCommandGetPassword = new Command(Resource.CMD_FORGOT_PASSWD);
    Form mBackForm;

    public ForgotPasswordForm(Form backForm) {
        super(Resource.CMD_REGISTER);

        mBackForm = backForm;

        Display.getInstance().setThirdSoftButton(false);
        setSelectCommandText("");
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        mEmail.setConstraint(TextField.EMAILADDR);
//        mEmail.setInputMode("abc");
        mEmail.setHint(Resource.TXT_EMAIL);
        mEmail.setPaintInputType(false);

        mButtonGetPassword.setAlignment(Button.CENTER);
        mButtonGetPassword.setPressedStyle(mButtonGetPassword.getSelectedStyle());
        mButtonGetPassword.addActionListener(this);

        addComponent();
    }

    void addComponent() {
        int paddingMarginLeft = mButtonGetPassword.getUnselectedStyle().getPadding(LEFT) + mButtonGetPassword.getUnselectedStyle().getMargin(RIGHT);
        //Panel Account
        Label lblEmail = new Label(Resource.TXT_EMAIL);
        Container panelCaption = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        panelCaption.addComponent(lblEmail);
        //panelCaption.getUnselectedStyle().setMargin(RIGHT, Display.getInstance().getDisplayWidth() / 10);

        Container panelText = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        panelText.addComponent(mEmail);

        if (Display.getInstance().getDisplayWidth() >= 320) {
            panelCaption.getUnselectedStyle().setMargin(LEFT, paddingMarginLeft);
            panelText.getUnselectedStyle().setMargin(RIGHT, paddingMarginLeft);
        }

        Container panelAcc = new Container(new BorderLayout());
        panelAcc.addComponent(BorderLayout.WEST, panelCaption);
        panelAcc.addComponent(BorderLayout.CENTER, panelText);

        addComponent(panelAcc);
        addComponent(mButtonGetPassword);
        addCommand(mCommandBack);
        addCommand(mCommandGetPassword);
        setBackCommand(mCommandBack);
        addCommandListener(this);
    }

    public void actionPerformed(ActionEvent evt) {
        if (evt.getSource() == mButtonGetPassword || evt.getCommand() == mCommandGetPassword) {
            doGetPasswd();
        } else if (evt.getCommand() == mCommandBack) {
            mBackForm.show();
        }
    }

    void doGetPasswd() {
        System.out.println("Get Passwd");
        if (StringUtil.isNullOrEmpty(mEmail.getText())) {
            DialogUtil.showErrorDialog(Resource.TXT_WARRING_NO_FILL_FULL_DATA);
            return;
        }
        //Check TextUser  + TextPasswd
        Thread t = new Thread() {

            public void run() {

                try {
                    AccountService mAccountService = new AccountService();
                    String message = mAccountService.doGetPassword(mEmail.getText());
                    System.out.println("UserID : " + Util.userID);
                    DialogUtil.showInfoDialog(message);
                    Indicator.hide();
                } catch (Exception ex) {
                    System.out.println("***************************Faild service Login");
                    ex.printStackTrace();
                    DialogUtil.showErrorConnectServer();
                    Indicator.hide();
//                    DialogUtil.showInfoDialog(Resource.LOGIN_FAILED);
                } finally {
                }
            }
        };
        t.setPriority(Thread.MAX_PRIORITY);
        Indicator.show(Resource.LOADING_MESSAGE, t);
    }

    class AccountService {

        final String USER_ID = "i";
        final String MESSAGE = "msg";
        private WebServicesParser parser;
        Service service;

        void LoginParser() {
        }

        public String doGetPassword(String email) throws Exception {
            service = new Service(Service.METHOD_LOGIN);
            service.addProperty("e", email);
            service.addProperty("y", "" + ItemEntity.TYPE_USER_GET_PASSWORD);

            parser = new WebServicesParser(service);

            //call service
            SoapObject response = parser.getResponse();
            System.out.println("[LicenceParser] response = " + response);
            String message = "";
            if (response != null) {
                SoapObject soap = (SoapObject) response.getProperty(0);

                message = parser.getProperty(soap, MESSAGE);
            }
            return message;
        }
    }
}
